package com.lenovo.study.service.impl;

import com.lenovo.study.service.RedisService;
import com.lenovo.study.service.SecKillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Author: fuxh3@lenovo.com
 * Date: Created in 2018/2/8 0008
 */
@Service
public class SecKillServiceImpl implements SecKillService{
    private static final Long stock_num = 10l;
    private static final String  goods_store = "goods_store:1";
    private static final String  order_1="order:1";
    @Autowired RedisService redisService;

    @Override public Boolean check_stock(String user_id) {
        Long lock =redisService.setnx("lock:key","1");
        if(1==lock){
            Long len = redisService.llen(goods_store);
            Long count = stock_num-len;
            for(int i=0;i<count;i++){
                redisService.lpush(goods_store,"1");
            }
        }
        String res = redisService.lpop(goods_store);
        if(StringUtils.isEmpty(res)){
            return false;
        }
        redisService.lpush(order_1,user_id);
        return true;
    }


}
